public class MyThread {
    public static int sum = 0;

    public void add() {
        synchronized (MyThread.class) {
            sum++;
        }
    }

    public static void main(String[] args) throws InterruptedException{
        Object A = new Object();
        Object B = new Object();
        MyThread mt = new MyThread();
        Thread t1 = new Thread(()-> {
            synchronized (A) {
                try {
                    //sleep 是为了给 t2 时间让它拿到 B
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (B) {
                    System.out.println("t1 拿到两把锁");
                }
            }

        });
        Thread t2 = new Thread(()-> {
            synchronized (B) {
                try {
                    //sleep 是为了给 t1 时间让它拿到 A
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (A) {
                    System.out.println("t2 拿到两把锁");
                }
            }

        });
        t1.start();
        t2.start();
    }
}
